package com.tiger.jms.sample2;

import javax.naming.*;
import javax.jms.*;

public class Receiver {
	public static void main(String[] args) {
		try {
			InitialContext ctx = new InitialContext();
			QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
			QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
			QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = (Queue) ctx.lookup("queue/sampleQueue");
			QueueReceiver queueReceiver = queueSession.createReceiver(queue);

			queueConnection.start();
			Message message = queueReceiver.receive();
			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				System.out.println("Message : " + textMessage.getText());
			} else {
				System.out.println("不明なメッセージ種別です");
			}

			queueReceiver.close();
			queueSession.close();
			queueConnection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
